#include "ParticleEmitter.h"
#define  GLUT_WHEEL_UP 3           //定义滚轮操作
#define  GLUT_WHEEL_DOWN 4
using namespace FireCloud;
GLfloat angle=0.0f;
GLfloat length = 32.0f;
GLuint fraem_number = 0;
GLuint particle_index = 0;
ParticleEmitterData main_ped;
ParticleEmitter *particle_emitter[3];
void ParticleEmitterDataInit1(ParticleEmitterData &ped){
	ped.life_max=180;
	ped.life_min=90;
	ped.particle_max_sum=20000;
	ped.emit_speed = 80.0f;
	ped.start_color_max.r = 1.0f;
	ped.start_color_max.g = 1.0f;
	ped.start_color_max.b = 1.0f;
	ped.start_color_max.a = 1.0f;
	ped.start_color_min.r = 0.8f;
	ped.start_color_min.g = 0.8f;
	ped.start_color_min.b = 1.0f;
	ped.start_color_min.a = 0.9f;
	ped.end_color_max.r = 0.2f;
	ped.end_color_max.g = 0.2f;
	ped.end_color_max.b = 0.8f;
	ped.end_color_max.a = 0.6f;
	ped.end_color_min.r = 0.0f;
	ped.end_color_min.g = 0.0f;
	ped.end_color_min.b = 0.4f;
	ped.end_color_min.a = 0.3f;
	ped.start_speed_min = 0.0f;
	ped.start_speed_max = 0.0f;
	ped.start_size_max = 0.4f;
	ped.start_size_min = 0.0f;
	ped.end_size_max = 0.8f;
	ped.end_size_min = 0.0f;
	ped.disperse_max = 0.04f;
	ped.acceleration.x = 0.003f;
	ped.acceleration.y = 0.003f;
	ped.acceleration.z = 0.0f;	
}
void ParticleEmitterDataInit2(ParticleEmitterData &ped){
	ped.life_max=120;
	ped.life_min=60;
	ped.particle_max_sum=20000;
	ped.emit_speed = 80.0f;
	ped.start_color_max.r = 1.0f;
	ped.start_color_max.g = 1.0f;
	ped.start_color_max.b = 0.5f;
	ped.start_color_max.a = 1.0f;
	ped.start_color_min.r = 0.8f;
	ped.start_color_min.g = 0.8f;
	ped.start_color_min.b = 0.1f;
	ped.start_color_min.a = 0.5f;
	ped.end_color_max.r = 0.9f;
	ped.end_color_max.g = 0.4f;
	ped.end_color_max.b = 0.4f;
	ped.end_color_max.a = 1.0f;
	ped.end_color_min.r = 0.9f;
	ped.end_color_min.g = 0.2f;
	ped.end_color_min.b = 0.2f;
	ped.end_color_min.a = 0.9f;
	ped.start_speed_min = 0.0f;
	ped.start_speed_max = 0.0f;
	ped.start_size_max = 0.4f;
	ped.start_size_min = 0.0f;
	ped.end_size_max = 0.8f;
	ped.end_size_min = 0.0f;
	ped.disperse_max = 0.05f;
	ped.acceleration.x = 0.0f;
	ped.acceleration.y = 0.003f;
	ped.acceleration.z = 0.0f;	
}
void ParticleEmitterDataInit3(ParticleEmitterData &ped){
	ped.life_max=180;
	ped.life_min=90;
	ped.particle_max_sum=20000;
	ped.emit_speed = 80.0f;
	ped.start_color_max.r = 1.0f;
	ped.start_color_max.g = 1.0f;
	ped.start_color_max.b = 1.0f;
	ped.start_color_max.a = 1.0f;
	ped.start_color_min.r = 0.8f;
	ped.start_color_min.g = 0.8f;
	ped.start_color_min.b = 1.0f;
	ped.start_color_min.a = 0.9f;
	ped.end_color_max.r = 0.2f;
	ped.end_color_max.g = 0.2f;
	ped.end_color_max.b = 0.8f;
	ped.end_color_max.a = 0.6f;
	ped.end_color_min.r = 0.0f;
	ped.end_color_min.g = 0.0f;
	ped.end_color_min.b = 0.4f;
	ped.end_color_min.a = 0.3f;
	ped.start_speed_min = 0.3f;
	ped.start_speed_max = 1.0f;
	ped.start_size_max = 0.4f;
	ped.start_size_min = 0.0f;
	ped.end_size_max = 0.8f;
	ped.end_size_min = 0.0f;
	ped.disperse_max = 0.08f;
	ped.acceleration.x = 0.0f;
	ped.acceleration.y = -0.008f;
	ped.acceleration.z = 0.0f;	
}
void glInit(){
	glEnable(GL_BLEND);
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	glEnableClientState(GL_VERTEX_ARRAY);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glClearColor(0.0,0.0,0.0,0.0);
}
void onMouse(int button, int state, int x, int y)
{
	if (state == GLUT_UP && button == GLUT_WHEEL_UP)                  
	{
		length += 1.0;
	}
	if (state == GLUT_UP && button == GLUT_WHEEL_DOWN)
	{
		length -= 1.0;
	}
	glutPostRedisplay();
}
void display(){
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-2,2,-2,2,2,100);
	glTranslatef(0.0f,0.0f,-length);
	glRotatef(45.0f,1.0f,0.0f,0.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glRotatef(angle,0.0f,1.0f,0.0f);
	particle_emitter[particle_index]->draw();
	++fraem_number;
	glutSwapBuffers();
}

void onTimer(int timerid){
	if(timerid==1){	
		glutPostRedisplay();
		glutTimerFunc(16,onTimer,1);
	}else if(timerid==2){	
		printf("FPS:%d\n",fraem_number);
		fraem_number=0;
		glutTimerFunc(1000,onTimer,2);
	}
}

void onKey(unsigned char c,int x,int y){
	if(c=='a'){
		angle += 2.0f;
	}else if(c=='d'){
		angle -= 2.0f;
	}else if(c>='1'&&c<='3'){
		particle_index = c-'1';
	}
}
int main(int argc,char** argv){
	ParticleEmitterDataInit1(main_ped);
	particle_emitter[0] = new ParticleEmitter(main_ped);//彗星
	ParticleEmitterDataInit2(main_ped);
	particle_emitter[1] = new ParticleEmitter(main_ped);//火焰
	ParticleEmitterDataInit3(main_ped);
	particle_emitter[2] = new ParticleEmitter(main_ped);//烟花
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
	glutInitWindowSize(600,600);
	glutCreateWindow("OpenGL Particle Engine");
	glutDisplayFunc(display);
	glutKeyboardFunc(onKey);
	glutMouseFunc(onMouse);
	glInit();
	glutTimerFunc(16,onTimer,1);
	glutTimerFunc(1000,onTimer,2);
	glutMainLoop();
	return 0;
}

